<?php

namespace App\Http\Controllers\Admin;

use App\Models\Manager;
use App\Traits\ExcelTrait;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ManagerController extends Controller
{
    use ExcelTrait;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $phone = $request->input('phone','');
        $name = $request->input('name','');
        $type = $request->input('type','');
        $company = $request->input('company','');
        $invitation_code = $request->input('invitation_code','');
        $create_time_start = $request->input('create_time_start','');
        $create_time_end = $request->input('create_time_end','');
        $status = $request->input('status','');
        $query = Manager::select();
        if($phone!=''){
            $query=$query->where('phone','=',$phone);
        }
        if($name!=''){
            $query=$query->where('name','like',"%$name%");
        }
        if($type!=0){
            $query=$query->where('type','=',$type);
        }
        if($company!=''){
            $query=$query->where('company','like',"%$company%");
        }
        if($invitation_code!=''){
            $query=$query->where('invitation_code','=',$invitation_code);
        }

        if($create_time_start!=''){
            $query=$query->where('created_at','>=',$create_time_start);
        }
        if($create_time_end!=''){
            $query=$query->where('created_at','<=',$create_time_end);
        }
        if($create_time_end!='' && $create_time_start!=''){
            if($create_time_start>$create_time_end){
                die('<script>alert("注册时间前值不能比后值大");history.back();</script>');
            }
            $query=$query->where('created_at','<=',$create_time_end)->where('created_at','>=',$create_time_start);
        }

        if($status!=0){
            $query=$query->where('status','=',$status);
        }

        $managers = $query->orderBy('created_at','desc')->paginate(10);
        $managers->appends(['phone'=>$phone,'name'=>$name,'type'=>$type,'company'=>$company,'invitation_code'=>$invitation_code,'create_time_start'=>$create_time_start,'create_time_end'=>$create_time_end,'status'=>$status]);
        $managers->phone = $phone;
        $managers->name = $name;
        $managers->type = $type;
        $managers->company = $company;
        $managers->invitation_code = $invitation_code;
        $managers->create_time_start = $create_time_start;
        $managers->create_time_end = $create_time_end;
        $managers->status=$status;


        return view('Admin.Manager.index',compact( 'managers'));
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
//        dd($request->all());
        if($request['free']=='free'){
            $res = Manager::where(['id'=>$id])->update(['status'=>1]);
            if(!$res){
                $this->set('code',1);
                $this->set('message','系统错误');
            }
        }elseif($request['free']=='freeze'){
            $res = Manager::where(['id'=>$id])->update(['status'=>2]);
            if(!$res){
                $this->set('code',1);
                $this->set('message','系统错误');
            }
        }elseif($request['free']=='manager_del'){
            $manager = Manager::findOrFail($id);
            $res = $manager->delete();
            if(!$res){
                $this->set('code',1);
                $this->set('message','系统错误');
            }
        }
        return $this->ajaxResponse();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 重置密码
     * @param Request $request
     */
    public function passwordReset(Request $request)
    {
        $manager = Manager::findOrFail($request->id);
        if($manager)
        {
            $phone_str = substr($manager->phone, -6);
            $bcrypt_str = bcrypt($phone_str);
             $update  = Manager::where(['id'=>$request->id])->update(['password'=>$bcrypt_str]);
            if (!$update) {
                $this->set('code',1);
                $this->set('message','系统错误');
            } else {
                //发送消息通知
//                $this->noticeUser($manager->phone);
            }
        }else{
            $this->set('code',1);
            $this->set('message','暂无数据');
        }
        return $this->ajaxResponse();
    }

    /**
     * 短信给用户反馈信息
     * @param $phone
     */
    public function noticeUser($phone)
    {

    }

    public function managerExport(Request $request){
        if($request->ids == 'all') {
            $title = '渠道用户-' . date('YmdHis', time());
            $manager = new Manager();
            $data = $manager->getExportData('all');
        }else{
            $ids = trim($request->ids, ',');
            $title = '渠道用户-' . date('YmdHis', time());
            $manager = new Manager();
            $data = $manager->getExportData('select',$ids);
        }
        $this->excelExport($title, $data);
    }

   /* public function daishu()
    {
        $title = '袋鼠用户数据';
        $daishu = new Daishu();
        $data = $daishu->getExportData();
        $this->excelExport($title, $data);
    }*/
}
